
//
//  LYPublishView.m
//  artist
//
//  Created by Jay_Apple on 16/3/7.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYPublishView.h"
#import "LYAngleTool.h"
#import "LYPublicPictureViewController.h"
#import "LYPublicVideoViewController.h"
#import "LYPublicClassesTableViewController.h"
#import "LYTabBarController.h"
#import "LYStudentDataViewController.h"
#import "LYPublicTextViewController.h"
#import "LYSelectedClassesViewController.h"
@interface LYPublishView ()

@property (nonatomic, copy) publishBlock block;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIButton *publishBtn;

@end

@implementation LYPublishView

+ (void)showWithBlock:(publishBlock)block{
    
    LYPublishView *view = [LYPublishView viewForNib];
    view.block = block;
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    view.frame = LYScreenBounds;
    UITabBarController *tabVC = (UITabBarController *)window.rootViewController;
    [window insertSubview:view belowSubview:tabVC.tabBar];

}

- (void)awakeFromNib{

    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self addGestureRecognizer:tap];
    
    //设置按钮
    [self.publishBtn setBackgroundImage:[[LYDataSourceManager sharedManager] getPublishBtnHighlightImage] forState:UIControlStateNormal];
}

/**
 *  手势响应
 */
- (void)tap:(UITapGestureRecognizer *)tap{
    
    

    //取出当前触摸点
    CGPoint tapPoint = [tap locationInView:self];
    
    if (tapPoint.y > (self.My_Height - LYTabBarHeight)) {
        [self dismiss];
        return;
    }
    
    //取出view中下点
    CGPoint targetPoint = CGPointMake(self.My_Width * 0.5, self.My_Height - LYTabBarHeight);
    
    //计算2点距离
    CGFloat distance = distanceBetweenPoints(tapPoint, targetPoint);
        //是否35<x<100
            //是
    if (distance > 35 && distance <= 100) {
            //计算2点水平角度
        CGFloat angle = angleBetweenPoints(tapPoint, targetPoint);
        
        CGFloat angleValue;
        if (angle > 0) {
            angleValue = -angle + 180;
        }else{
        
            angleValue = -angle;
        }
        
        publishType type = angleValue / 180 * 4;
        LYUserInfoModel *userInfo = [VVYSaveDataManager getDataWithKey:KEY_USERMESSAGE];
        switch (type) {
            case publishTypeText:
            {

                
                LYPublicTextViewController *public = [[LYPublicTextViewController alloc] init];
                [self puchToViewController:public];

            }

                break;
            case publishTypePicture:{
                LYPublicPictureViewController *publicVC =  [[LYPublicPictureViewController alloc]init];
                [self puchToViewController:publicVC];
            }
             break;
            case  publishTypeSubject:
            {
                if([userInfo.extra_info.auth_identity isEqualToString:@"0"]){
                    [SVProgressHUD showErrorWithStatus:@"没通过认证,不能发布课程"];
                    [self dismiss];

                    return;
                }
                
                
                
                LYPublicClassesTableViewController *publicVC =  [[LYPublicClassesTableViewController alloc]init];
                [self puchToViewController:publicVC];
            }

                break;
            case publishTypeVideo:{
                if([userInfo.extra_info.auth_identity isEqualToString:@"0"]){
                    [SVProgressHUD showErrorWithStatus:@"没有通过认证，不能发布视频"];
                    [self dismiss];
                    return;
                }
                LYPublicVideoViewController *publicVC =  [[LYPublicVideoViewController alloc]init];
                [self puchToViewController:publicVC];
            }
              break;
        }
    }else{
    
        [self dismiss];
    }
}

- (void)puchToViewController:(UIViewController*)viewController{
    
    [self dismiss];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    LYTabBarController *tab = (LYTabBarController *)window.rootViewController;
    UINavigationController *nav =  tab.selectedViewController;
    [nav pushViewController:viewController animated:YES];
    
}

- (void)didMoveToSuperview{
    
    [super didMoveToSuperview];
    for (NSInteger i = 0; i<self.contentView.subviews.count; i++) {
        UIImageView *imageV = self.contentView.subviews[i];
        imageV.alpha = 0;
        [UIView animateKeyframesWithDuration:0.25 delay:0.1 * i options:0 animations:^{
            imageV.alpha = 1;
        } completion:nil];
    }
}

- (IBAction)dismiss{

    [self removeFromSuperview];
}
@end
